<template>
	<view class="buycart-main">
		<checkbox-group @change="ckChange">
			<view class="product-card" v-for="item in productList" :key="item.tempId">
				<view class="info-block">
					<label>
						<checkbox :value="item.temId+''" :checked="checkbox.includes(item.tempId)" /><text></text>
					</label>
					<navigator class="nav" url="/pages/buyCart/index">
						<image class="cover-img" :src="item.coverImg" mode=""></image>
						<view class="info-text">
							<view class="gname">{{item.gname}}</view>
							<view class="code">
								<text class="code1">编号:{{item.skuCode}}</text>
								<text>价格:￥{{item.price}}</text>
							</view>
							<view>数量:{{item.count}}</view>
						</view>
					</navigator>
				</view>
				<!-- <div class="buy-count-steps">
					<button @click="upt({id:item.id,val:-1})">-</button>
					{{item.count}}
					<button @click="upt({id:item.id,val:+1})">+</button>
				</div> -->
				<view class="buy-count-steps">
					<text class="step-reduce" @tap="btnReduce">-</text>
					<text class="step-text">{{item.count}}</text>
					<text class="step-add" @tap="btnAdd">+</text>
				</view>
			</view>
		</checkbox-group>
		<view class="footer-bar">
			<view class="sel-all"> 
				<checkbox-group @change="selAll">
					<label class="sel1">
						<text>全选</text>
						<checkbox value="0" />
					</label> 
				</checkbox-group> 
				<text>合计:￥{{totalPrice}}</text>
			</view>
			<view class="sel2">
				<text class="btn" @click="goPay">结算</text>
			</view>
		</view>
	</view>
</template> 

<script>
	export default {
		name: 'BuyCart',
		data() {
			return {
				productList: [],
				checkbox: [],
				isSelAll: false
			}
		},
		onLoad() {
			this.productList = uni.getStorageSync('leju-buycart');
		},
		computed: {
			totalPrice() {
				return this.productList.reduce((total, current) => {

					return total = total + current.count * current.price
					// } else {
					// 	return total
					// 	//不return出去的话，total 没有值，后面没法计算
					// }
				}, 0)
			}
		},
		methods: {
			btnReduce() {

			},
			btnAdd() {

			},
			ckChange(e) {
				console.log(e.detail)
				this.checkbox = e.detail.value;
			},
			selAll(e) {
				console.log(e.deatil);
				var value = e.detail.value;
				if (value.length > 0) {
					this.productList.forEach(item => { //forEach() 方法用于调用数组的每个元素，并将元素传递给回调函数。
						this.checkbox.push(item.tempId)
					})
				} else {
					this.checkbox = []
				}
			},
			goPay() {
				var skuList = [];
				for (var i = 0; i < this.checkbox.length; i++) {
					var tempId = this.checkbox[i];
					var sku = this.productList.find(item => (item.tempId == tempId));
					console.log('sku...', sku);
					if (sku) {
						skuList.push(sku);
					}
				}
				var totalCount = 0;
				var totalPrice = 0;
				skuList.forEach(item => {
					totalCount += item.count;
					totalPrice += item.price;
				});
				//构建订单对象
				var orderObj = {
					userId: this.$store.getters.userId,
					count: totalCount, //skucount
					freight: 0, //运费
					price: totalPrice, //总价格
					orderDetailList: skuList
				}
				//存入vuex
				this.$store.commit('order/ADD_ORDER_TEMP', orderObj)
				//跳转到订单确认页面
				uni.navigateTo({
					url: '/pages/mine/order/detail/index?from=cart'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.buycart-main {
		.product-card {
			width: 672rpx;
			margin: 50rpx auto;
			background-color: #fff;
			border-radius: 24rpx;
			padding: 20rpx 20rpx;
			box-sizing: border-box;

			.info-block {
				display: flex;
				justify-content: flex-start; //左对齐
				align-items: center;

				.nav {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.cover-img {
						width: 136rpx;
						height: 136rpx;
						flex-shrink: 1;
					}

					.info-text {
						font-size: 22rpx;
						color: #8D8D8D;
						margin-left: 46rpx;

						.gname {
							font-family: PingFangSC-Semibold;
							font-size: 26rpx;
							color: #3E3E3E;
							letter-spacing: 0.93px;
						}

						.price {
							display: flex;
							justify-content: space-between;
							align-items: center;
						}

						.code1 {
							margin-right: 80rpx;
						}
					}
				}
			}

			.buy-count-steps {
				display: flex;
				justify-content: flex-end;

				.step-reduce,
				.step-add,
				.step-text {
					width: 60rpx;
					height: 60rpx;
					background: #F1ECE7;
					border-radius: 50%;
					text-align: center;
					line-height: 60rpx;
					font-size: 30rpx;
					margin: 0 6rpx;
				}

				.step-text {
					width: 90rpx;
					border-radius: 30rpx;
				}
			}
		}

		.footer-bar {
			position: fixed;
			display: flex;
			justify-content: space-between;
			align-items: center;
			left: 0;
			bottom: 0;
			width: 100%;
			height: 150rpx;
			background-color: #fff;
			padding: 0 40rpx;
			box-sizing: border-box;

			.sel-all {
				display: flex;
				align-items: center;

				.sel1 {
					margin-right: 30rpx;
				}
			}

			.btn {
				display: inline-block;
				width: 208rpx;
				height: 80rpx;
				background: #354E44;
				border-radius: 14px;
				color: #fff;
				font-size: 36rpx;
				line-height: 80rpx;
				text-align: center;
			}
		}
	}
</style>
